//
//  DiscoveryViewController.m
//  Wifi
//
//  Created by srulos on 2017/6/9.
//  Copyright © 2017年 srulos. All rights reserved.
//

#import "DiscoveryViewController.h"
#import "WifiHeader.h"
#import "DiscoveryTableViewController.h"
#import "AppDelegate.h"

@interface DiscoveryViewController ()
{
    NSMutableArray * m_arrayCategories;
    NSMutableArray * m_arrayLabel;
    NSMutableArray * m_arrayControllers;
    DiscoveryTableViewController * m_currentDiscoveryTableViewController;
}
-(UIColor *)colorFromHexRGB:(NSString *) inColorString;
@end

@implementation DiscoveryViewController

- (instancetype)initWithNibName:(nullable NSString *)nibNameOrNil bundle:(nullable NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        NSString * title = K_NSLOCAL(@"Found");
        self.title = title;
        self.tabBarItem = [[UITabBarItem alloc]initWithTitle:title image:[UIImage imageNamed:@"tb_find_nol"] selectedImage:[UIImage imageNamed:@"tb_find_sel"]];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserverForName:kStatusBarTappedNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
        if (m_currentDiscoveryTableViewController != nil) {
            [m_currentDiscoveryTableViewController autoScrollToTop];
        }
    }];
    
    self.dataSource = self;
    self.delegate = self;
    
    m_arrayLabel = [[NSMutableArray alloc]init];
    m_arrayCategories = [[NSMutableArray alloc]init];
    m_arrayControllers = [[NSMutableArray alloc]init];
    
    if (IS_CHINESE) {//中文的请求百思不得姐
        [self getCategoriesFromLocal];
    }
    else{//国外的请求leancloud
        [self getCategoriesFromLeanCloud];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - ViewPagerDataSource
- (NSUInteger)numberOfTabsForViewPager:(ViewPagerController *)viewPager {
    return m_arrayCategories.count;
}

- (UIView *)viewPager:(ViewPagerController *)viewPager viewForTabAtIndex:(NSUInteger)index {
    AVObject * object = [m_arrayCategories objectAtIndex:index];
    
    UILabel *label = [[UILabel alloc]init];
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont systemFontOfSize:14.0];
    label.text = [object objectForKey:@"title"];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [self colorFromHexRGB:@"666666"];
    [label sizeToFit];
    [m_arrayLabel addObject:label];
    return label;
}

- (UIViewController *)viewPager:(ViewPagerController *)viewPager contentViewControllerForTabAtIndex:(NSUInteger)index {
    return [m_arrayControllers objectAtIndex:index];//此处直接返回
}

#pragma mark - ViewPagerDelegate
- (CGFloat)viewPager:(ViewPagerController *)viewPager valueForOption:(ViewPagerOption)option withDefault:(CGFloat)value {
    
    switch (option) {
        case ViewPagerOptionStartFromSecondTab:
            return 0.0;
            break;
        case ViewPagerOptionCenterCurrentTab:
            return 0.0;
            break;
        case ViewPagerOptionTabLocation:
            return 1.0;
            break;
        case ViewPagerOptionTabHeight:
            return 35.0;
            break;
        case ViewPagerOptionTabWidth:
        {
            if(IS_IPAD){
                return 72.0f;
            }
            else{
                CGRect rx = [UIScreen mainScreen ].bounds;
                return rx.size.width / m_arrayCategories.count;
            }
        }
            
        default:
            break;
    }
    
    return value;
}

- (UIColor *)viewPager:(ViewPagerController *)viewPager colorForComponent:(ViewPagerComponent)component withDefault:(UIColor *)color {
    
    switch (component) {
        case ViewPagerIndicator:
            return [self colorFromHexRGB:@"10aeff"];
        case ViewPagerTabsView:
        {
            return [UIColor whiteColor];
        }
        case ViewPagerContent:
        {
            return [self colorFromHexRGB:@"666666"];
        }
        default:
            break;
    }
    
    return color;
}

- (void)viewPager:(ViewPagerController *)viewPager didChangeTabToIndex:(NSUInteger)index {
    for (int i = 0 ; i < m_arrayLabel.count; i++) {
        UILabel * label = [m_arrayLabel objectAtIndex:i];
        if (i == index) {
            label.textColor = [self colorFromHexRGB:@"10aeff"];
        }
        else
        {
            label.textColor = [self colorFromHexRGB:@"666666"];
        }
    }
    
    //下标溢出bug
    m_currentDiscoveryTableViewController = [m_arrayControllers objectAtIndex:index];
}

-(UIColor *)colorFromHexRGB:(NSString *) inColorString
{
    UIColor *result = nil;
    unsigned int colorCode = 0;
    unsigned char redByte, greenByte, blueByte;
    
    if (nil != inColorString)
    {
        NSScanner *scanner = [NSScanner scannerWithString:inColorString];
        (void) [scanner scanHexInt:&colorCode]; // ignore error
    }
    redByte = (unsigned char) (colorCode >> 16);
    greenByte = (unsigned char) (colorCode >> 8);
    blueByte = (unsigned char) (colorCode); // masks off high bits
    result = [UIColor
              colorWithRed: (float)redByte / 0xff
              green: (float)greenByte/ 0xff
              blue: (float)blueByte / 0xff
              alpha:1.0];
    return result;
}

#pragma mark getCategories
-(void)getCategoriesFromLeanCloud
{
    AVQuery *query = [AVQuery queryWithClassName:@"DiscoverCategory"];
    [query findObjectsInBackgroundWithBlock:^(NSArray * _Nullable objects, NSError * _Nullable error) {
        if (objects != nil && objects.count > 0) {
            [m_arrayCategories addObjectsFromArray:objects];
            
            //直接一开始就建好vc，需要的时候从array取给他，修复下标溢出bug
            for (int i = 0 ; i < m_arrayCategories.count; i++) {
                AVObject * object = [m_arrayCategories objectAtIndex:i];
                
                VC_INIT_WITH_NIB_NAME(DiscoveryTableViewController);
                vc.m_title = [object objectForKey:@"title"];
                
                [m_arrayControllers addObject:vc];
            }
            
            [self reloadData];
        }
    }];
}

-(void)getCategoriesFromLocal
{
    AVObject * object1 = [[AVObject alloc]init];
    [object1 setObject:K_NSLOCAL(@"Recom") forKey:@"title"];
    [m_arrayCategories addObject:object1];
 
    AVObject * object2 = [[AVObject alloc]init];
    [object2 setObject:K_NSLOCAL(@"Video") forKey:@"title"];
    [m_arrayCategories addObject:object2];
 
    AVObject * object3 = [[AVObject alloc]init];
    [object3 setObject:K_NSLOCAL(@"Image") forKey:@"title"];
    [m_arrayCategories addObject:object3];
    
    //直接一开始就建好vc，需要的时候从array取给他，修复下标溢出bug
    for (int i = 0 ; i < m_arrayCategories.count; i++) {
        AVObject * object = [m_arrayCategories objectAtIndex:i];
        
        VC_INIT_WITH_NIB_NAME(DiscoveryTableViewController);
        vc.m_title = [object objectForKey:@"title"];
        NSLog(@"------- %@",vc.m_title);
        [m_arrayControllers addObject:vc];
    }
    
    [self reloadData];
}

@end
